package hotel.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import hotel.dao.DaoManager;
import hotel.dao.RequestDao;
import hotel.dao.base.ConnectionBehavior;
import hotel.dao.base.DaoCommand;
import hotel.entity.RequestEntity;
import hotel.model.IRequest;
import hotel.model.Request;
import hotel.service.RequestService;

public class RequestServiceImpl implements RequestService 
{

	private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
	private static final String REQUESTSERVICEIMPL_ERROR = "Error in RequestServiceImpl:";
	
	private DaoManager daoManager;
	
	public RequestServiceImpl(DaoManager daoManager) 
	{
		this.daoManager = daoManager;
	}
	
	protected DaoManager getDaoManager() 
	{
		return daoManager;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<IRequest> getRequests() 
	{
		return (List<IRequest>)
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final RequestDao requestDao = manager.getDao(RequestDao.class);
					    	List<RequestEntity> listRequestEntity = null;
					    	try
					    	{
					    		listRequestEntity = requestDao.findAll();
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	final List<IRequest> listRequest = new ArrayList<IRequest>();
					    	for (final RequestEntity requestEntity : listRequestEntity) 
					    	{
					    		listRequest.add(convertToModel(requestEntity));
					    	}
			  		
					    	return listRequest;
						}
					}, ConnectionBehavior.CLOSE);
	}


	@Override
	public IRequest getById(final int id)
	{
		return (IRequest)
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final RequestDao requestDao = manager.getDao(RequestDao.class);
					    	RequestEntity requestEntity = null;
					    	try
					    	{
					    		requestEntity = requestDao.findById(id);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	IRequest request = null;
					    		request = convertToModel(requestEntity);
			  		
					    	return request;
						}
					}, ConnectionBehavior.CLOSE);
	}
	

	@Override
	public void insert(final String roomClass, final int beds, final String name, final String surname,
			final String username) 
	{
			getDaoManager().command(new DaoCommand()
				{
					public Object execute(final DaoManager manager) 
					{
						final RequestDao requestDao = manager.getDao(RequestDao.class);
				    	try
				    	{
				    		requestDao.insert(new RequestEntity(0,roomClass, beds, name, surname, username));
				    	}
				    	catch (Exception e) 
				    	{
				    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
				    	}
				    	return null;
					}
				}, ConnectionBehavior.CLOSE);
	}

	@Override
	public void deleteRequest(final int id) 
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final RequestDao requestDao = manager.getDao(RequestDao.class);
		    	try
		    	{
		    		requestDao.delete(id);
		    	}
		    	catch (SQLException e) 
		    	{
		    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
		    	} catch (Exception e) {
		    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
				}
		    	
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);
		
	}
	
	@Override
	public void changeStatus(final int id, final String status) 
	{
		getDaoManager().command(new DaoCommand()
		{
			public Object execute(final DaoManager manager) 
			{
				final RequestDao requestDao = manager.getDao(RequestDao.class);
		    	try
		    	{
		    		requestDao.changeStatus(id, status);
		    	}
		    	catch (SQLException e) 
		    	{
		    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
		    	} catch (Exception e) {
		    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
				}
		    	
		    	return null;
			}
		}, ConnectionBehavior.CLOSE);
		
	}
	
	@Override
	public String checkStatus(final int id)
	{
		return (String) 
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final RequestDao requestDao = manager.getDao(RequestDao.class);
							String status = null;
					    	try
					    	{
					    		status = requestDao.checkStatus(id);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	return status;
						}
					}, ConnectionBehavior.CLOSE);
	}
	
	public IRequest getByUsername(final String username)
	{
		return (IRequest)
				getDaoManager().command(new DaoCommand()
					{
						public Object execute(final DaoManager manager) 
						{
							final RequestDao requestDao = manager.getDao(RequestDao.class);
					    	RequestEntity requestEntity = null;
					    	try
					    	{
					    		requestEntity = requestDao.findByUsername(username);
					    	}
					    	catch (SQLException e) 
					    	{
					    		logger.error(REQUESTSERVICEIMPL_ERROR + e);
					    	}
					  		
					    	IRequest request = null;
					    		request = convertToModel(requestEntity);
			  		
					    	return request;
						}
					}, ConnectionBehavior.CLOSE);

	}
	
	private IRequest convertToModel(RequestEntity requestEntity) 
	{
		IRequest request = new Request(
					requestEntity.getId(),
					requestEntity.getRoomClass(),
					requestEntity.getBeds(),
					requestEntity.getName(),
					requestEntity.getSurname(),
					requestEntity.getUsername());
		return request;
	}


}

